package com.itzxx.huazhun.ui.register

import android.text.TextUtils
import com.itzxx.huazhun.data.repository.HttpRequestCoroutine
import com.itzxx.huazhun.utils.UserCacheHelper
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.callback.databind.StringObservableField
import com.itzxx.library_base_ktx.ext.request
import java.util.TreeMap
import java.util.regex.Pattern

/**
 * Desc:
 *
 * Author: zhangxiaoxiao
 * Date: 2024/6/25 13:49
 * Copyright: Copyright (c) 2006-2024
 * Company: @海晟融创
 * Updater:
 * Update Time:
 * Update Comments:
 */
class RegisterViewModel : BaseViewModel() {



    val REGEX_MOBILE: Pattern =
        Pattern.compile("^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[0,1,4-9])|(?:5[0-3,5-9])|(?:6[2,5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[0-3,5-9]))\\d{8}$")

    /** 用户名（英文、数字）*/
    val REGEX_NAME: String = "[[a-zA-Z]|\\d]*"

    /** 中文（普通的中文字符） */
    val REGEX_CHINESE: String = "[\\u4e00-\\u9fa5]*"

    //机构全称
    var mechanism = StringObservableField()

    //申请人姓名
    var applicant = StringObservableField()

    //职务
    var post = StringObservableField()


    //登录名
    var account = StringObservableField()

    //手机号码
    var phone = StringObservableField()

    //设置密码
    var password = StringObservableField()

    //机构所在地
    var mechanismArea = StringObservableField()


    /**
     * 申请注册
     */
    fun doRegister() {
        //清除已经存在问题
        //默认初始化为空
        UserCacheHelper.putToken("")
        UserCacheHelper.putTenantId("")
        if (TextUtils.isEmpty(mechanism.get())) {
            toast("请输入机构名称")
        }else if (TextUtils.isEmpty(applicant.get())) {
            toast("请输入申请人姓名")
        }else if (!REGEX_CHINESE.toRegex().matches(applicant.get())) {
            toast("输入的申请人姓名不符合要求")
        } else if (TextUtils.isEmpty(post.get())) {
            toast("请输入职务")
        }else if (!REGEX_CHINESE.toRegex().matches(applicant.get())) {
            toast("输入的职务名称不符合要求")
        } else if (TextUtils.isEmpty(account.get())) {
            toast("请输入登录名")
        } else if (!REGEX_NAME.toRegex().matches(account.get())) {
            toast("输入的登录名不符合要求")
        } else if (TextUtils.isEmpty(phone.get())){
            toast("请输入手机号")
        }else if (!REGEX_MOBILE.matcher(phone.get()).matches()){
            toast("请输入正确手机号")
        }else if (TextUtils.isEmpty(password.get())){
            toast("请输入密码")
        }else if (!REGEX_NAME.toRegex().matches(phone.get())){
            toast("请输入符合要求的密码")
        }else if (TextUtils.isEmpty(mechanismArea.get())){
            toast("请输入机构所在地")
        } else {
            loadingChange.showDialog.postValue("注册中...")
            val empty = TreeMap<String, Any>()
            empty.put("tenantName",mechanism.get())
            empty.put("applicationName",applicant.get())
            empty.put("postName",post.get())
            empty.put("username",account.get())
            empty.put("mobile",phone.get())
            empty.put("password",password.get())
            empty.put("address",mechanismArea.get())
            request({
                HttpRequestCoroutine.userRegister(
                    empty
                )
            }, {
                toast("注册成功")
                baseContext.value?.let {
                    finish()
                }
                loadingChange.dismissDialog.postValue(false)
            }, {
                toast(it.errorMsg)
                loadingChange.dismissDialog.postValue(false)
            })
        }
    }

}